import sys
sys.path.insert(0,'../')
from RestAPI.amazon.S3 import  S3Request,S3session,Bucket,FileObject,UploadPolicy
from RestAPI.core.APIExceptions import *
import unittest
import setting

class testForm(unittest.TestCase):
    def setUp(self):
        self.session=S3session()
        self.session.setCredential(setting.AUTHID,setting.AUTHKEY)
        self.bucket=self.session.createBucketObj('feignwolf-test')
    def testGetForm(self):
        print "test get upload form\n===================================================="
        req=S3Request("johnsmith.s3.amazonaws.com").POST("/")
        req.timestamp=lambda : 'Tue, 27 Mar 2007 19:42:41 +0000'
        req.authorize('AKIAIOSFODNN7EXAMPLE','wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY')
        policy=UploadPolicy()
        policy.expires('1 minute')
        #TODO policy.expires('2007-12-01T12:00:00.000Z') not implemented
        policy.condition('bucket','==','johnsmith')
        policy.condition('key','~=','user/eric/')
        policy.condition('acl','==','public-read')
        policy.condition('success_action_redirect','==','http://johnsmith.s3.amazonaws.com/successful_upload.html')
        policy.condition('Content-Type','~=','image/')
        policy.condition('x-amz-meta-uuid','eq','14365123651274')
        policy.condition('x-amz-meta-tag','starts-with','')
        #policy.condition('acl','==','public-read')
        print policy.json()
        print req.getForm('/*',policy)
        print self.bucket.createUploadForm(policy)


if __name__ == '__main__':
    unittest.main()
